package com.example.clxy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.clxy.dao.QuestionMapper;
import com.example.clxy.entity.Question;
import com.example.clxy.entity.dto.QuestionDto;
import com.example.clxy.entity.dto.QuestionDto2;
import com.example.clxy.result.PageResult;
import com.example.clxy.result.PageResultForQuestion;
import com.example.clxy.result.WebRestResult;
import com.example.clxy.result.business.PageResult.ExperimentPageResult;
import com.example.clxy.result.business.PageResult.QuestionPageResult;
import com.example.clxy.result.business.QuestionResult;
import com.example.clxy.service.QuestionService;
import com.example.clxy.util.UUIDUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;

/**
 * @author Hsieh
 * @title: QuestionServiceImpl
 * @projectName clxy
 * @description:
 * @date 2020/8/520:36
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    QuestionMapper questionMapper;

    @Override
    public Map<String,Object> addQuestion(Question question) {
//        WebRestResult result = new WebRestResult();
        Map<String,Object> result = new HashMap<>() ;
        String questionid = UUIDUtil.randomUUID();
        question.setQuestionid(questionid);
        question.setFlag("0");
        question.setCreatedTime(new Date());
        question.setUpdatedTime(new Date());
        if (questionMapper.insert(question)==1){
//            result.setResult(WebRestResult.SUCCESS);
            result.put("result",WebRestResult.SUCCESS);
            result.put("questionid",questionid);
            result.put("message","添加成功！");
//            result.setMessage("添加成功！");
        }else{
//            result.setResult(WebRestResult.FAILURE);
//            result.setMessage("添加失败！");
            result.put("result",WebRestResult.FAILURE);
            result.put("questionid",null);
            result.put("message","添加失败！");
        }
        return result;
    }

    @Override
    public WebRestResult deleteQuestion(String questionid) {
        WebRestResult result = new WebRestResult();
        if (questionMapper.deleteByPrimaryKey(questionid)==1
                &&(questionMapper.deleteQuestionInExperimentbyquestionid(questionid)!=0
                    ||questionMapper.deleteQuestionInPlatformbyquestionid(questionid)!=0)){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除成功！");
        }else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除失败！");
        }
        return result;
    }


    @Override
    public WebRestResult updateQuestion(@RequestBody QuestionDto2 questionDto2) {
        WebRestResult result=new WebRestResult();
        questionDto2.setUpdatedTime(new Date());
        questionDto2.setFlag("0");
        String questionid = questionDto2.getQuestionid();
        questionDto2.setFlag("0");
        questionDto2.setUpdatedTime(new Date());
        String experimentid = questionDto2.getExperimentid();
        String platformid = questionDto2.getPlatformid();
        if (questionMapper.updateByPrimaryKey(questionDto2)==1){
            if (experimentid !=null) {
                questionMapper.ExperimentbindQuestion(experimentid,questionid);
            }
            if (platformid!=null){
                questionMapper.PlatformbindQuestion(platformid,questionid);
            }
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("修改成功！");
        }else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("修改失败！");
        }
        return result;
    }

    @Override
    public PageResult<QuestionPageResult> getQuestionList(String content,int page, int pagesize) {
        PageResult<QuestionPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<QuestionPageResult> list =questionMapper.getQuestionList(content, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取试题列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取试题列表失败！");
        }
        return result;
    }

    @Override
    public WebRestResult ExperimentbindQuestion(String experimentid,String questionid) {
        WebRestResult result = new WebRestResult();
        if (questionMapper.ExperimentbindQuestion(experimentid,questionid)!=0){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("操作成功！");
        }else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("操作失败！");
        }
        return result;
    }

    @Override
    public WebRestResult PlatformbindQuestion(String platformid,String questionid) {
        WebRestResult result = new WebRestResult();
        if (questionMapper.PlatformbindQuestion(platformid,questionid)!=0){
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("操作成功！");
        }else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("操作失败！");
        }
        return result;
    }

    @Override
    public PageResult<QuestionPageResult> getQuestionListbyExperiment(String experimentid,String content,int page, int pagesize) {
        PageResult<QuestionPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<QuestionPageResult> list =questionMapper.getQuestionListbyExperiment(experimentid, content, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取实验试题列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取实验试题列表失败！");
        }
        return result;
    }


    @Override
    public PageResult<QuestionPageResult> getQuestionListbyPlatform(String platformid,String content, int page, int pagesize) {
        PageResult<QuestionPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<QuestionPageResult> list =questionMapper.getQuestionListbyPlatform(platformid, content, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取平台试题列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取平台试题列表失败！");
        }
        return result;
    }


    @Override
    public PageResult<QuestionPageResult> getallQuestionListwithExperiment(String content, int page, int pagesize) {
        PageResult<QuestionPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<QuestionPageResult> list =questionMapper.getallQuestionListwithExperiment(content, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取实验试题列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取实验试题列表失败！");
        }
        return result;
    }

    @Override
    public PageResult<QuestionPageResult> getallQuestionListwithPlatform(String content, int page, int pagesize) {
        PageResult<QuestionPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<QuestionPageResult> list =questionMapper.getallQuestionListwithPlatform(content, begin, end, pagesize);
        if (list.size() > 0) {
            result.setTotal(list.get(0).getCnt());
            result.setPageCount(list.get(0).getPage());
            result.setRows(list);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取平台试题列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取平台试题列表失败！");
        }
        return result;
    }

    @Override
    public QuestionResult getQuestionbyId(String questionid) {
        QuestionResult questionResult= new QuestionResult();
        QuestionResult result=questionMapper.getQuestionbyId(questionid);
        List<String> experimentid=questionMapper.getQuestionExperimentid(questionid);
        List<String> platformid=(questionMapper.getQuestionPlatformid(questionid));
        if(result!=null){
            questionResult=result;
            questionResult.setExperimentid(experimentid);
            questionResult.setPlatformid(platformid);
            questionResult.setResult(WebRestResult.SUCCESS);
            questionResult.setMessage("获取成功！");
        }else {
            questionResult.setResult(WebRestResult.FAILURE);
            questionResult.setMessage("获取失败！");
        }
        return questionResult;
    }

    @Override
    public WebRestResult addQuestionMuti(QuestionDto questionDto) {
        WebRestResult result = new WebRestResult();
        Question question = new Question();
        questionDto.setQuestionid(UUIDUtil.randomUUID());
        BeanUtils.copyProperties(questionDto, question);
        question.setFlag("0");
        question.setCreatedTime(new Date());
        question.setUpdatedTime(new Date());
        if (questionMapper.insert(question) == 1) {
            if (questionDto.getPlatformlist() == null && questionDto.getExperimentlist() == null) {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("请勾选至少一个平台或者实验！");
                return result;
            }
            if (questionDto.getPlatformlist() != null && questionDto.getPlatformlist().size() > 0) {
                questionMapper.insertQuestionMutiPlatform(questionDto);
            }
            if (questionDto.getExperimentlist() != null && questionDto.getExperimentlist().size() > 0) {
                questionMapper.insertQuestionMutiExperiment(questionDto);
            }
            result.setMessage("新增试题成功");
            result.setResult(WebRestResult.SUCCESS);
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("新增试题失败");
        }
        return result;
    }

    @Override
    public WebRestResult updateQuestionMuti(QuestionDto questionDto) {
        WebRestResult result = new WebRestResult();
        Question question = new Question();
        BeanUtils.copyProperties(questionDto, question);
        question.setUpdatedTime(new Date());

        if (questionMapper.updateByPrimaryKeySelective(question) == 1) {
            if (questionDto.getPlatformlist() == null && questionDto.getExperimentlist() == null) {
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("请勾选至少一个平台或者实验！");
                return result;
            }
            if (questionDto.getPlatformlist() != null && questionDto.getPlatformlist().size() > 0) {
                questionMapper.deleteQuestionMutiPlatform(questionDto.getQuestionid());
                questionMapper.insertQuestionMutiPlatform(questionDto);
            }
            if (questionDto.getExperimentlist() != null && questionDto.getExperimentlist().size() > 0) {
                questionMapper.deleteQuestionMutiExperiment(questionDto.getQuestionid());
                questionMapper.insertQuestionMutiExperiment(questionDto);
            }
            result.setMessage("更新试题成功");
            result.setResult(WebRestResult.SUCCESS);
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("更新试题失败");
        }
        return result;
    }

    @Override
    public PageResult<QuestionPageResult> getQuestionApartmently(String platformid, String experimentid, int page, int pagesize) {
        PageResult<QuestionPageResult> result = new PageResult<>();
        int begin = (page - 1) * pagesize;
        int end = pagesize;
        List<QuestionPageResult> experimentQuestionlist = new ArrayList<>();
        List<QuestionPageResult> platformQuestionlist = new ArrayList<>();

        if (platformid!=null&&experimentid!=null&&(!platformid.equals(""))) {
            experimentQuestionlist = questionMapper.getQuestionListbyExperimentProportionally(
                    experimentid,
                    null,
                    begin,
                    end,
                    pagesize);

            platformQuestionlist = questionMapper.getQuestionListbyPlatformProportionally(
                    platformid,
                    null,
                    begin,
                    end,
                    pagesize);
            if ((experimentQuestionlist.size())<5){
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("实验试题不足，请补充试题");
            return result;
            }
            if (platformQuestionlist.size()<5){
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("平台试题不足，请补充试题");
            return result;
            }
        }
        if ((platformid.equals("")||platformid==null)&&experimentid!=null){

             experimentQuestionlist = questionMapper.getQuestionListOnlybyExperiment(experimentid, null, begin, end, pagesize);
            if ((experimentQuestionlist.size())<10){
                result.setResult(WebRestResult.FAILURE);
                result.setMessage("实验试题不足，请补充试题");
                return result;
            }
        }

        List<QuestionPageResult> questionlist= new ArrayList<>();

        if (platformQuestionlist.size()==0)
        {
            for (int i = 0;i < 10; i++){
                questionlist.add(experimentQuestionlist.get(i));
            }
        }
        if (platformQuestionlist.size()!=0)
            for (int i = 0;i < 5; i++){
                questionlist.add(experimentQuestionlist.get(i));
                questionlist.add(platformQuestionlist.get(i));

            }

        if (questionlist.size() > 0) {
            result.setTotal(questionlist.get(0).getCnt());
            result.setPageCount(questionlist.get(0).getPage());
            result.setRows(questionlist);
            System.out.println(questionlist);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("获取实验试题列表成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("获取实验试题列表失败！");
        }
        return result;
    }

    @Override
    public WebRestResult isPassSafetyAssessment(String scheduleid, String studentid, String status) {
        WebRestResult result = new WebRestResult();
        if (status.equals("2")){
            questionMapper.isPassSafetyAssessment(scheduleid, studentid, status);
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("恭喜通过考核！现在可以开始实验了！请注意人身安全！");
        }
        if (status.equals("1")){
            questionMapper.isPassSafetyAssessment(scheduleid, studentid, status);
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("安全第一！请认真学习后再来参与实验！");
        }
        if (status.equals("0")){
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("请先答题再开始实验！");
        }
        return result;
    }
}

